﻿/// <reference path="../../extjs/ext-all-debug.js" />
Ext.define('yzmh.controller.Links', {
    extend: 'Ext.app.Controller',

    stores: ['Links'],

    models: ['Link'],

    views: ['link.List', 'link.Edit'],

    refs: [{
        ref: 'tbar',
        selector: 'linklist toolbar'
    }, {
        ref: 'grid',
        selector: 'linklist'
    }],

    init: function () {
        this.control({
            'linklist': {
                render: this.loadLink,
                itemdblclick: this.editLink
            },
            'linklist button[action=add]': {
                click: this.addLink
            },
            'linklist button[action=delete]': {
                click: this.deleteLink
            },
            'linkedit button[action=save]': {
                click: this.saveLink
            },
            'linklist dataview': {
                drop: this.sortLink
            }
        });
    },

    sortLink:function (node, data, dropRec, dropPosition) {
        var oldid = [], newid = [], store = this.getLinksStore();
        Ext.Msg.confirm("提示", "你需要立即执行排序吗？", function (result) {
            if (result == "yes") {
                store.each(function (record) {
                    oldid.push(record.get('id'));
                    newid.push(record.get('id'));
                });
                oldid.sort(function (a, b) { return a - b; });
                var data = [];
                store.each(function (record, i) {
                    if (oldid[i] != newid[i]) {
                        data.push(Ext.apply(record.raw, {
                            id: oldid[i]
                        }));
                    }
                });
                Ext.Ajax.request({
                    url: 'data/link.aspx?a=sort',
                    params: { data: Ext.encode(data) },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            store.reload();
                        } else {
                            Ext.Msg.alert("错误", result.errorMessage);
                        }
                    }
                });
            }
        });
    },

    addLink: function (grid, record) {
        var edit = Ext.create('yzmh.view.link.Edit').show();
    },

    editLink: function (grid, record) {
        var edit = Ext.create('yzmh.view.link.Edit').show();
        edit.down('form').loadRecord(record);
    },

    deleteLink: function () {
        var rows = this.getGrid().getSelectionModel().getSelection();
        var store = this.getLinksStore();
        var ids = [];
        Ext.each(rows, function (r) {
            ids.push(r.get("id"));
        });
        Ext.Msg.confirm("提示", "确定要删除选中的" + rows.length + "个友情链接吗？", function (result) {
            if (result == "yes") {
                Ext.Ajax.request({
                    url: 'data/link.aspx?a=delete',
                    params: { ids: ids.join(',') },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            store.remove(rows);
                        } else {
                            Ext.Msg.alert("错误", result.message);
                        }
                    }
                });
            }
        });
    },

    saveLink: function (button, e) {
        var win = button.up('window'),
            form = win.down('form'),
            record = form.getRecord(),
            values = form.getValues(),
            store = this.getLinksStore();
        if (form.getForm().isValid()) {
            form.submit({
                url: 'data/link.aspx?a=save',
                success: function (form, action) {
                    win.close();
                    store.reload();
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failed', action.result ? action.result.errorMessage : 'No response');
                }
            });
        }
    },

    loadLink: function (grid) {
        this.getLinksStore().load();
    }
});
